####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A32, ... A35
###
####################################################################################


library(dplyr)
library(AER)
library(rlist)
library(ggplot2)


################################################################################
# Set Up
################################################################################


# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))

################################################################################
# Plot Functions
################################################################################

# Plot
makeplot = function(model_output, hete_var){
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  container = lapply(model_output, function(x) rbind(cbind(x[2:6,], period = c(-4, -3, -2, 1, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=6))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
  }


# Select samples for different bandwidths
selectsample = function(bandwidth){
  df = df_did[abs(df_did$running)<bandwidth,]
  df$imm_quant_99 = cut(df$imm_share_99,
                          breaks=c(quantile(df$imm_share_99, probs = seq(0, 1, by = 0.33333), na.rm=T)),
                          labels=c("1","2","3"), include.lowest=T)
  return(df)
}
  

################################################################################
# Figure A32
################################################################################

df = selectsample(2500)

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df[which(!is.na(df$fn) & df$imm_quant_99==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA32.pdf'), height=7, width=10)


################################################################################
# Figure A33
################################################################################

df = selectsample(1500)

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df[which(!is.na(df$fn) & df$imm_quant_99==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA33.pdf'), height=7, width=10)


################################################################################
# Figure A34
################################################################################

df = selectsample(1000)

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df[which(!is.na(df$fn) & df$imm_quant_99==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA34.pdf'), height=7, width=10)


################################################################################
# Figure A35
################################################################################

df = selectsample(800)

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df[which(!is.na(df$fn) & df$imm_quant_99==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA35.pdf'), height=7, width=10)


